#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/7/31 14:53
# @USER    : Shengji He
# @File    : PowerSet.py
# @Software: PyCharm
# @Version  : Python-
# @TASK:
from typing import List


class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        """
        Write a method to return all subsets of a set. The elements in a set are pairwise distinct.

        Note: The result set should not contain duplicated subsets.

        Example:
            Input:  nums = [1,2,3]
            Output:
                [
                  [3],
                  [1],
                  [2],
                  [1,2,3],
                  [1,3],
                  [2,3],
                  [1,2],
                  []
                ]

        :param nums:
        :return:
        """
        ans = [[]]
        for i in range(1, len(nums) + 1):
            cb = self.uniqueCombinations(nums, i)
            while True:
                try:
                    ans.append(next(cb))
                except StopIteration:
                    break
        return ans

    def uniqueCombinations(self, items, n):
        def afterIthItem(items, i):
            return items[i + 1:]

        return self._combinators(afterIthItem, items, n)

    def _combinators(self, _handle, items, n):
        if n == 0:
            yield []
        for i, item in enumerate(items):
            this_one = [item]
            for cc in self._combinators(_handle, _handle(items, i), n - 1):
                yield this_one + cc


if __name__ == '__main__':
    S = Solution()
    nums = [1, 2, 3]
    print(S.subsets(nums))
    print('done')
